package ModelLayer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class EmployeeRecords {
	
	private ArrayList<Employee> employees;
	private static EmployeeRecords instance = null;

	private EmployeeRecords() {
		employees = new ArrayList<Employee>();
	}
	
	public static EmployeeRecords getInstance()
    {
        if(instance == null)
        {
            instance = new EmployeeRecords();
        }
        return instance;
    }	
	
	public Employee getEmployee(int id) throws RealException {		
		Employee theEmployee = null;
        boolean found = false;
        Iterator<Employee> it = employees.iterator();
        while(it.hasNext() && !found)
        {
            Employee employee = it.next();
            if(employee.getId() == id)
            {
                theEmployee = employee;
                found = true;
            }	
        }
        if(!found) throw new RealException("Employee was not found");
        return theEmployee;
	}
	
	public ArrayList<Employee> getEmployees()
	{
		return employees;
	}
	
	public void removeEmployee(int id) throws RealException
    {        
        Employee employee = getEmployee(id);
        if(employee != null)
        {
            employees.remove(employee);
        }
    }
	
	public void updateEmployee(int id ,int choise, String newValue) throws RealException
	{
		Employee employee=getEmployee(id);
		if(employee!=null)
		{
			switch(choise)
			{
				case 1:
				{
					employee.setName(newValue);
					break;
				}
				case 2:
				{
					employee.setAddress(newValue);
					break;
				}
				case 3:
				{
					employee.setPhone(Long.parseLong(newValue));
					break;
				}
				case 4:
				{
					employee.setPassword(newValue);
					break;		
				}
				case 5:
				{
					employee.setPosition(newValue);
					break;		
				}
			}	
		}
	}

    public void addEmployee(String name, String address, long phone, String password, String position)
    {   
    	Employee employee = new Employee(name, address, phone, password, position);
        employees.add(employee);
    }
    
    public String printEmployee(int id) throws RealException {
		return getEmployee(id).toString();
	}

    public ArrayList<String> getAllEmployees() {
		ArrayList<String> texts = new ArrayList<String>();
		for (Employee employee : employees) {
			texts.add(employee.toString());
		}
		return texts;
	}
    
    
    public int login(String name, String password) throws RealException
    {
    	Employee theEmployee = null;
        boolean found = false;
        Iterator<Employee> it = employees.iterator();
        while(it.hasNext() && !found)
        {
            Employee employee = it.next();
            if(employee.getName().equals(name) && employee.getPassword().equals(password))
            {
                theEmployee = employee;
                found = true;
            }	
        }
        if(!found) throw new RealException("Login invalid");
        return theEmployee.getId();
    }
    
    public int loginGUI(String name, char[] password) throws RealException
    {
    	Employee theEmployee = null;
        boolean found = false;
        Iterator<Employee> it = employees.iterator();
        while(it.hasNext() && !found)
        {
            Employee employee = it.next();
            if(employee.getName().equals(name) && (Arrays.equals(employee.getPassword().toCharArray(), password)))
            {
                theEmployee = employee;
                found = true;
            }	
        }
        if(!found) throw new RealException("Login invalid");
        return theEmployee.getId();
    }
    
    public void checkEmployee(int id) throws RealException {		
        boolean found = false;
        Iterator<Employee> it = employees.iterator();
        while(it.hasNext() && !found)
        {
            Employee employee = it.next();
            if(employee.getId() == id)
            {
                found = true;
            }	
        }
        if(!found) throw new RealException("Employee was not found");
	}
    

	@SuppressWarnings("unchecked")
	public void load(ObjectInputStream obj_in) throws IOException, ClassNotFoundException
	{
		this.employees = (ArrayList<Employee>) obj_in.readObject();	
		Employee.setLastId(((Integer)obj_in.readObject()).intValue());
	}
	
	public void save(ObjectOutputStream obj_out) throws IOException
	{		
		obj_out.writeObject(employees);
		obj_out.writeObject(new Integer(Employee.getLastId()));
	}

	public int getTotalEmployees() {
		return employees.size();
	}

	public String getEmployeePosition(int loggedEmployeeId) throws RealException {
		Employee employee = getEmployee(loggedEmployeeId);
		return employee.getPosition();
	}

}
